package jp.co.brilliantservice.app.sos;

/*
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
*/

//import jp.co.brilliantservice.app.openar.data.PoiObject;
//import jp.co.brilliantservice.utility.SdLog;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WidgetProvider extends AppWidgetProvider {
	private static final int BUTTON_DIRECT = 0;
    private static int count = 0;
    private static int beatCount = 0;
    private static PendingIntent alarmIntent = null;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		RemoteViews view = buildUpdate(context, -1);

		for (int i = 0; i < appWidgetIds.length; i++) {
			appWidgetManager.updateAppWidget(appWidgetIds[i], view);
		}
	}

	static RemoteViews buildUpdate(Context context, int appWidgetId) {
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
		views.setOnClickPendingIntent(R.id.buttonDirect, getLaunchPendingIntent(context, appWidgetId, BUTTON_DIRECT));
		return views;
	}

	public static PendingIntent getLaunchPendingIntent(Context context, int appWidgetId,
			int buttonId) {
		PendingIntent pi = PendingIntent.getBroadcast(context, 0, getLaunchIntent(context, appWidgetId, buttonId), 0);
		return pi;
	}
	
	public static Intent getLaunchIntent(Context context,int appWidgetId,int buttonId){
        Intent launchIntent = new Intent();
        launchIntent.setClass(context, WidgetProvider.class);
        launchIntent.addCategory(Intent.CATEGORY_ALTERNATIVE);
        launchIntent.setData(Uri.parse("custom:" + buttonId));
        return launchIntent;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		if (intent.hasCategory(Intent.CATEGORY_ALTERNATIVE)) {
			SettingManager mPref = new SettingManager(context);
			Uri data = intent.getData();
			int buttonId = Integer.parseInt(data.getSchemeSpecificPart());
			if (buttonId == BUTTON_DIRECT) {
                if (alarmIntent!=null) {
                    /* 以前のアラームをキャンセル */
                    ((AlarmManager)context.getSystemService(Context.ALARM_SERVICE)).cancel(alarmIntent);
                }
                if (mPref.getBoolean("complete")) {
                    beatCount = 0;
                    mPref.setBoolean("complete",false);
                }
                beatCount++;
                Intent it = new Intent(context, BeatTimerReceiver.class);
                if (mPref.getBoolean("use_directmessage")) {
                    it.putExtra("target", mPref.get("target_account"));
                } else {
                    it.putExtra("public", true);
                }
                String pattern = "pattern"+(mPref.get("current_pattern"));
                String text = mPref.get(pattern);
                String[] texts = text.split("\n");
                it.putExtra("text", texts[count]);
                count = (count + 1) % texts.length;
                /* RTCアラームを設定 */
                alarmIntent = PendingIntent.getBroadcast(
                        context
                        , 0
                        , it
                        , 0);
                ((AlarmManager)context.getSystemService(Context.ALARM_SERVICE)).set(
                        AlarmManager.RTC
                        , System.currentTimeMillis()+500
                        , alarmIntent
                        );
			}
		}
	}
}
